<?php
  class infomationController extends Yaf\Controller_Abstract{
        public function localInfoAction()
        {
            session_start();
//            获取用户ID
            $id = $_SESSION['uid'];
            $user = UsersModel::where('Id', '=', $id)->select()->get();
            if (count($user) > 0) {
                $u = json_decode($user, true);
                $username = $u[0]['username'];
                $sex = $u[0]['sex'];
                $tel = $u[0]['tel'];
                $email = $u[0]['email'];
                $introduce = $u[0]['introduce'];
                $this->getView()->assign('username', $username);
                $this->getView()->assign('sex', $sex);
                $this->getView()->assign('tel', $tel);
                $this->getView()->assign('email', $email);
                $this->getView()->assign('introduce', $introduce);
            }
        }

//
        public function checkedAction(){
            Yaf\Dispatcher::getInstance()->disableView();
                $name = $_POST['username'];
                $sex = $_POST['sex'];
                $tel = $_POST['tel'];
                $email = $_POST['email'];
                $introduce = $_POST['introduce'];
                session_start();
    //            获取用户ID
                $id = $_SESSION['uid'];
    //            echo $id;

                        //         对个人简介进行敏感词过滤
    //           定义简单的敏感词数组
                        $list = ['我去','我丢','我靠','我草','我擦','傻逼','傻屌','煞笔','傻狗'];
    //           替换后的数据
                        $newIntro = $this->sensitive($list,$introduce);
    //                    echo $newIntro;
                    $isFill = UsersModel::where('Id','=',$id)->update(['username'=>$name,'sex'=>$sex,'tel'=>$tel,'email'=>$email,'introduce'=>$newIntro]);
//                   echo "数据库执行结果：".$isFill;
                    if ($isFill>0){
                        $msg = "更改成功";
                        $flag=true;
                    }else{
                        $msg = "更改失败";
                        $flag=false;
                    }
                    $arr=array("msg"=>$msg,"flag"=>$flag);
                    echo json_encode($arr);
         }
//       简单的敏感词过滤，后期可以试着使用数据结构中的前缀树对敏感词进行过滤，空间换时间
    function sensitive($list, $string){
        $count = 0; //违规词的个数
        $sensitiveWord = ''; //违规词
        $stringAfter = $string; //替换后的内容
        $pattern = "/".implode("|",$list)."/i"; //定义正则表达式
        if(preg_match_all($pattern, $string, $matches)){ //匹配到了结果
            $patternList = $matches[0]; //匹配到的数组
            $count = count($patternList);
            $sensitiveWord = implode(',', $patternList); //敏感词数组转字符串
            $replaceArray = array_combine($patternList,array_fill(0,count($patternList),'*')); //把匹配到的数组进行合并，替换使用
            $stringAfter = strtr($string, $replaceArray); //结果替换
        }
        return $stringAfter;
    }
  }